/**
 *
 */
package jp.heartdokyun;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import jp.heartdokyun.model.Heart;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * @author impaler This is the main surface that handles the ontouch events and
 *         draws the image to the screen.
 */
public class MainGamePanel extends SurfaceView implements
		SurfaceHolder.Callback {

	private static final String TAG = MainGamePanel.class.getSimpleName();

	private MainThread thread;
	private List<Heart> mListHearts = new ArrayList<Heart>();
	// private String type, level;
	private Bitmap image;
	Handler thandler;
	Boolean trun;
	String tlevel;
	public static int nextrate = 60;
	public static String getLevel;

	// private RectF mRectF;

	public MainGamePanel(Context context, String type, String level) {
		super(context);
		tlevel = level;
		getLevel = level;

		// adding the callback (this) to the surface holder to intercept events
		getHolder().addCallback(this);

		// bg bitmap
		image = BitmapFactory
				.decodeResource(getResources(), R.drawable.game_bg);

		// create droid and load bitmap
		int speed_a = getSpeedA(level);
		int speed_b = getSpeedB(level);
		int speed_c = getSpeedC(level);
		int speed_d = getSpeedD(level);
		int speed_e = getSpeedE(level);
		int speed_f = getSpeedF(level);

		int distance_a1 = getDistanceA(level);
		int distance_a2 = getDistanceA(level);
		int distance_a3 = getDistanceA(level);
		int distance_a4 = getDistanceA(level);
		int distance_a5 = getDistanceA(level);

		int distance_b1 = getDistanceB(level);
		int distance_b2 = getDistanceB(level);
		int distance_b3 = getDistanceB(level);
		int distance_b4 = getDistanceB(level);
		int distance_b5 = getDistanceB(level);

		int angleSpeed = 5; // A and B
		int distance = 300; // C and D

		if (level.equals("1")) {
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 25, 25, 0,
					speed_a, angleSpeed, distance_a1));
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 25, 200, 1,
					speed_b, angleSpeed, distance_b1));
			mytimer4(level, speed_e, angleSpeed, distance);
			mytimer5(level, speed_e, angleSpeed, distance);

			/*
			 * mListHearts.add(new Heart(BitmapFactory.decodeResource(
			 * getResources(), R.drawable.ic_heart_gold), BitmapFactory
			 * .decodeResource(getResources(), R.drawable.ic_heart_gold_touch),
			 * 75, 0, 4, speed_e, angleSpeed, distance));
			 */

		} else if (level.equals("2")) {
			// B, 2su, mini heart same motion LV1 goldheart
			/*
			 * mListHearts.add(new Heart(BitmapFactory.decodeResource(
			 * getResources(), R.drawable.ic_heart_normal), BitmapFactory
			 * .decodeResource(getResources(),
			 * R.drawable.ic_heart_normal_touch), 25, 25, 0, speed_a,
			 * angleSpeed, distance_a2));
			 */
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 125, 125, 1,
					speed_b, angleSpeed, distance_b2));
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 150, 150, 3,
					speed_d, angleSpeed, distance));
			mytimer2(level, speed_f, angleSpeed, distance);
			// To do add timer
			/*
			 * mListHearts.add(new Heart(BitmapFactory.decodeResource(
			 * getResources(), R.drawable.ic_heart_small), BitmapFactory
			 * .decodeResource(getResources(), R.drawable.ic_heart_small), 175,
			 * 0, 5, speed_f, angleSpeed, distance));
			 */

		} else if (level.equals("3")) {
			// A,3us, gold heart wait 15sec LV1speed plus little
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 25, 25, 0,
					speed_a, angleSpeed, distance_a3));
			mytimer(level, speed_e, angleSpeed, distance);
			/*
			 * mListHearts.add(new Heart(BitmapFactory.decodeResource(
			 * getResources(), R.drawable.ic_heart_normal), BitmapFactory
			 * .decodeResource(getResources(),
			 * R.drawable.ic_heart_normal_touch), 125, 125, 1, speed_b,
			 * angleSpeed, distance_b3));
			 * 
			 * mListHearts.add(new Heart(BitmapFactory.decodeResource(
			 * getResources(), R.drawable.ic_heart_small), BitmapFactory
			 * .decodeResource(getResources(),
			 * R.drawable.ic_heart_normal_touch), 50, 50, 2, speed_c,
			 * angleSpeed, distance));
			 */
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 150, 0, 2,
					speed_d, angleSpeed, distance));
			/*
			 * mListHearts.add(new Heart(BitmapFactory.decodeResource(
			 * getResources(), R.drawable.ic_heart_gold), BitmapFactory
			 * .decodeResource(getResources(), R.drawable.ic_heart_gold_touch),
			 * 75, 0, 4, speed_e, angleSpeed, distance));
			 */

		} else if (level.equals("4")) {
			// A,B,3us mini heart same speed LV3
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 25, 25, 0,
					speed_a, angleSpeed, distance_a4));
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 125, 125, 1,
					speed_b, angleSpeed, distance_b4));
			/*
			 * mListHearts.add(new Heart(BitmapFactory.decodeResource(
			 * getResources(), R.drawable.ic_heart_small), BitmapFactory
			 * .decodeResource(getResources(),
			 * R.drawable.ic_heart_normal_touch), 50, 50, 2, speed_c,
			 * angleSpeed, distance));
			 */
			mytimer2(level, speed_f, angleSpeed, distance);
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 150, 150, 2,
					speed_d, angleSpeed, distance));
			/*
			 * mListHearts.add(new Heart(BitmapFactory.decodeResource(
			 * getResources(), R.drawable.ic_heart_gold), BitmapFactory
			 * .decodeResource(getResources(), R.drawable.ic_heart_gold_touch),
			 * 75, 0, 4, speed_e, angleSpeed, distance));
			 * 
			 * mListHearts.add(new Heart(BitmapFactory.decodeResource(
			 * getResources(), R.drawable.ic_heart_gold), BitmapFactory
			 * .decodeResource(getResources(), R.drawable.ic_heart_gold_touch),
			 * 175, 0, 5, speed_f, angleSpeed, distance));
			 */

		} else if (level.equals("5")) {
			// A,B C move 0 ,D move 0 goldheart same speed LV4 miniheart at
			// 20sec
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 25, 25, 0,
					speed_a, angleSpeed, distance_a5));
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 125, 125, 1,
					speed_b, angleSpeed, distance_b5));
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 50, 200, 2,
					speed_c, angleSpeed, 300));
			/*
			 * mListHearts.add(new Heart(BitmapFactory.decodeResource(
			 * getResources(), R.drawable.ic_heart_normal), BitmapFactory
			 * .decodeResource(getResources(),
			 * R.drawable.ic_heart_normal_touch), 150, 200, 3, speed_d,
			 * angleSpeed, 0));
			 */

			mytimer(level, speed_f, angleSpeed, distance);
			mytimer3(level, speed_e, angleSpeed, distance);
			/*
			 * mListHearts.add(new Heart(BitmapFactory.decodeResource(
			 * getResources(), R.drawable.ic_heart_gold), BitmapFactory
			 * .decodeResource(getResources(), R.drawable.ic_heart_gold_touch),
			 * 75, 0, 4, speed_e, angleSpeed, distance)); mListHearts.add(new
			 * Heart(BitmapFactory.decodeResource( getResources(),
			 * R.drawable.ic_heart_gold), BitmapFactory
			 * .decodeResource(getResources(), R.drawable.ic_heart_gold_touch),
			 * 175, 0, 5, speed_f, angleSpeed, distance));
			 */

		} else {
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 25, 25, 0,
					speed_a, angleSpeed, distance));
			mListHearts.add(new Heart(BitmapFactory.decodeResource(
					getResources(), R.drawable.ic_heart_normal), BitmapFactory
					.decodeResource(getResources(),
							R.drawable.ic_heart_normal_touch), 125, 125, 1,
					speed_b, angleSpeed, distance));
		}

		// mListHearts.add(new
		// Heart(BitmapFactory.decodeResource(getResources(),
		// R.drawable.ic_heart_normal), 25, 25, 0, speed_a));
		// mListHearts.add(new
		// Heart(BitmapFactory.decodeResource(getResources(),
		// R.drawable.ic_heart_normal), 125, 125, 1, speed_b));
		// mListHearts.add(new
		// Heart(BitmapFactory.decodeResource(getResources(),
		// R.drawable.ic_heart_small), 50, 50, 2, speed_c));
		// mListHearts.add(new
		// Heart(BitmapFactory.decodeResource(getResources(),
		// R.drawable.ic_heart_small), 150, 150, 3, speed_d));
		// mListHearts.add(new
		// Heart(BitmapFactory.decodeResource(getResources(),
		// R.drawable.ic_heart_gold), 75, 75, 4, speed_e));
		// mListHearts.add(new
		// Heart(BitmapFactory.decodeResource(getResources(),
		// R.drawable.ic_heart_gold), 175, 175, 5, speed_f));

		// create the game loop thread
		thread = new MainThread(getHolder(), this);
		// this.type = type;
		// this.level = level;

		// make the GamePanel focusable so it can handle events
		setFocusable(true);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// at this point the surface is created and
		// we can safely start the game loop
		thread.setRunning(true);
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "Surface is being destroyed");
		// tell the thread to shut down and wait for it to finish
		// this is a clean shutdown
		boolean retry = true;
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				// try again shutting down the thread
			}
		}
		Log.d(TAG, "Thread was shut down cleanly");
		recycleAllHeartBitmap();
		this.mListHearts.clear();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			// delegating event handling to the droid
			Iterator<Heart> itHeart = this.mListHearts.iterator();
			while (itHeart.hasNext())
				itHeart.next().handleActionDown((int) event.getX(),
						(int) event.getY());

			// check if in the lower part of the screen we exit
			// if (event.getY() > getHeight() - 50) {
			// thread.setRunning(false);
			// ((Activity) getContext()).finish();
			// } else {
			// Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
			// }

		}
		// if (event.getAction() == MotionEvent.ACTION_MOVE) {
		// the gestures
		// if (mHeart.isTouched()) {
		// // the droid was picked up and is being dragged
		// mHeart.setX((int) event.getX());
		// mHeart.setY((int) event.getY());
		// }
		// }
		// if (event.getAction() == MotionEvent.ACTION_UP) {
		// touch was released
		// if (mHeart.isTouched()) {
		// mHeart.setTouched(false);
		// }
		// }
		return true;
	}

	public void render(Canvas canvas) {
		// canvas.drawColor(getResources().getColor(R.color.pink));
		float right = (float) canvas.getWidth();
		float bottom = (float) canvas.getHeight();
		RectF mRectF = new RectF(0.0F, 0.0F, right, bottom);
		canvas.drawBitmap(image, null, mRectF, null);
		Iterator<Heart> itHeart = this.mListHearts.iterator();
		while(itHeart.hasNext()) {
			Heart heart = itHeart.next();
			if (heart.isAvail())
				heart.draw(canvas);
		}
	}

	private void mytimer(String level, final int speed, final int angleSpeed,
			final int distance) {
		final int mx = (int) (Math.random() * 350);
		Timer timer;
		timer = new Timer(true);
		thandler = new Handler();
		timer.schedule(new TimerTask() {
			public void run() {
				// 表示の更新
				thandler.post(new Runnable() {
					public void run() {
						// handler.postDelayed(this, REPEAT_INTERVAL);

						mListHearts.add(new Heart(BitmapFactory.decodeResource(
								getResources(), R.drawable.ic_heart_gold),
								BitmapFactory.decodeResource(getResources(),
										R.drawable.ic_heart_gold_touch), mx, 0,
								5, speed, angleSpeed, distance));
					}
				});
			}
		}, 14000, 14000);

	}

	private void mytimer4(String level, final int speed, final int angleSpeed,
			final int distance) {
		final int mx = (int) (Math.random() * 350);
		Timer timer;
		timer = new Timer(true);
		thandler = new Handler();
		timer.schedule(new TimerTask() {
			public void run() {
				// 表示の更新
				thandler.post(new Runnable() {
					public void run() {
						// handler.postDelayed(this, REPEAT_INTERVAL);

						mListHearts.add(new Heart(BitmapFactory.decodeResource(
								getResources(), R.drawable.ic_heart_gold),
								BitmapFactory.decodeResource(getResources(),
										R.drawable.ic_heart_gold_touch), mx, 0,
								5, speed, angleSpeed, distance));
					}
				});
			}
		}, 10000);

	}

	private void mytimer5(String level, final int speed, final int angleSpeed,
			final int distance) {
		final int mx = (int) (Math.random() * 350);
		Timer timer;
		timer = new Timer(true);
		thandler = new Handler();
		timer.schedule(new TimerTask() {
			public void run() {
				// 表示の更新
				thandler.post(new Runnable() {
					public void run() {
						// handler.postDelayed(this, REPEAT_INTERVAL);

						mListHearts.add(new Heart(BitmapFactory.decodeResource(
								getResources(), R.drawable.ic_heart_gold),
								BitmapFactory.decodeResource(getResources(),
										R.drawable.ic_heart_gold_touch), mx, 0,
								5, speed, angleSpeed, distance));
					}
				});
			}
		}, 20000);

	}

	private void mytimer2(String level, final int speed, final int angleSpeed,
			final int distance) {
		final int mx = (int) (Math.random() * 350);
		Timer timer;
		timer = new Timer(true);
		thandler = new Handler();
		timer.schedule(new TimerTask() {
			public void run() {
				// 表示の更新
				thandler.post(new Runnable() {
					public void run() {
						// handler.postDelayed(this, REPEAT_INTERVAL);

						mListHearts.add(new Heart(BitmapFactory.decodeResource(
								getResources(), R.drawable.ic_heart_small),
								BitmapFactory.decodeResource(getResources(),
										R.drawable.ic_heart_small_touch), mx,
								0, 4, speed, angleSpeed, distance));
					}
				});
			}
		}, 14000, 14000);

	}

	private void mytimer3(String level, final int speed, final int angleSpeed,
			final int distance) {
		final int mx = (int) (Math.random() * 350);
		Timer timer;
		timer = new Timer(true);
		thandler = new Handler();
		timer.schedule(new TimerTask() {
			public void run() {
				// 表示の更新
				thandler.post(new Runnable() {
					public void run() {
						// handler.postDelayed(this, REPEAT_INTERVAL);

						mListHearts.add(new Heart(BitmapFactory.decodeResource(
								getResources(), R.drawable.ic_heart_small),
								BitmapFactory.decodeResource(getResources(),
										R.drawable.ic_heart_small_touch), mx,
								0, 4, speed, angleSpeed, distance));
					}
				});
			}
		}, 20000);

	}

	/**
	 * This is the game update method. It iterates through all the objects and
	 * calls their update method if they have one or calls specific engine's
	 * update method.
	 */
	public void update(Canvas canvas) {
		// // check collision with right wall if heading right
		// if (mHeart.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT
		// && mHeart.getX() + mHeart.getBitmap().getWidth() / 2 >= getWidth()) {
		// mHeart.getSpeed().toggleXDirection();
		// }
		// // check collision with left wall if heading left
		// if (mHeart.getSpeed().getxDirection() == Speed.DIRECTION_LEFT
		// && mHeart.getX() - mHeart.getBitmap().getWidth() / 2 <= 0) {
		// mHeart.getSpeed().toggleXDirection();
		// }
		// // check collision with bottom wall if heading down
		// if (mHeart.getSpeed().getyDirection() == Speed.DIRECTION_DOWN
		// && mHeart.getY() + mHeart.getBitmap().getHeight() / 2 >= getHeight())
		// {
		// mHeart.getSpeed().toggleYDirection();
		// }
		// // check collision with top wall if heading up
		// if (mHeart.getSpeed().getyDirection() == Speed.DIRECTION_UP
		// && mHeart.getY() - mHeart.getBitmap().getHeight() / 2 <= 0) {
		// mHeart.getSpeed().toggleYDirection();
		// }
		// // Update the lone droid
		Iterator<Heart> itHeart = this.mListHearts.iterator();
		while (itHeart.hasNext())
			itHeart.next().update(canvas);
	}

	public void stopGame() {
		thread.setRunning(false);
	}

	private void recycleAllHeartBitmap() {
		Iterator<Heart> itHeart = this.mListHearts.iterator();
		while (itHeart.hasNext())
			itHeart.next().recycleBitmap();
	}

	private int getSpeedA(String level) {
		int speed = 5;
		if (level.equals("1")) {
			speed = 3;
		} else if (level.equals("2")) {
			speed = 6;
		} else if (level.equals("3")) {
			speed = 8;
		} else if (level.equals("4")) {
			speed = 12;
		} else if (level.equals("5")) {
			speed = 13;
		}

		return speed;

	}

	private int getDistanceA(String level) {
		int dis = 10;
		if (level.equals("1")) {
			dis = 10;
		} else if (level.equals("2")) {
			dis = 18;
		} else if (level.equals("3")) {
			dis = 30;
		} else if (level.equals("4")) {
			dis = 50;
		} else if (level.equals("5")) {
			dis = 51;
		}

		return dis;

	}

	private int getDistanceB(String level) {
		int dis = 10;
		if (level.equals("1")) {
			dis = 10;
		} else if (level.equals("2")) {
			dis = 20;
		} else if (level.equals("3")) {
			dis = 30;
		} else if (level.equals("4")) {
			dis = 40;
		} else if (level.equals("5")) {
			dis = 50;
		}

		return dis;

	}

	private int getSpeedB(String level) {
		int speed = 5;
		if (level.equals("1")) {
			speed = 2;
		} else if (level.equals("2")) {
			speed = 5;
		} else if (level.equals("3")) {
			speed = 6;
		} else if (level.equals("4")) {
			speed = 10;
		} else if (level.equals("5")) {
			speed = 10;
		}

		return speed;

	}

	private int getSpeedC(String level) {
		int speed = 0;
		if (level.equals("1")) {
			speed = 0;
		} else if (level.equals("2")) {
			speed = 0;
		} else if (level.equals("3")) {
			speed = 1;
		} else if (level.equals("4")) {
			speed = 1;
		} else if (level.equals("5")) {
			speed = 1;
		}

		return speed;

	}

	private int getSpeedD(String level) {
		int speed = 0;
		if (level.equals("1")) {
			speed = 0;
		} else if (level.equals("2")) {
			speed = 1;
		} else if (level.equals("3")) {
			speed = 1;
		} else if (level.equals("4")) {
			speed = 1;
		} else if (level.equals("5")) {
			speed = 1;
		}

		return speed;

	}

	private int getSpeedE(String level) {
		int speed = 0;
		if (level.equals("1")) {
			speed = 5;
		} else if (level.equals("2")) {
			speed = 0;
		} else if (level.equals("3")) {
			speed = 10;
		} else if (level.equals("4")) {
			speed = 10;
		} else if (level.equals("5")) {
			speed = 15;
		}

		return speed;

	}

	private int getSpeedF(String level) {
		int speed = 0;
		if (level.equals("1")) {
			speed = 0;
		} else if (level.equals("2")) {
			speed = 5;
		} else if (level.equals("3")) {
			speed = 0;
		} else if (level.equals("4")) {
			speed = 10;
		} else if (level.equals("5")) {
			speed = 15;
		}

		return speed;
	}

	private int getDistanceCD(String level) {
		int dis = 300;
		if (level.equals("1")) {
			dis = 300;
		} else if (level.equals("2")) {
			dis = 300;
		} else if (level.equals("3")) {
			dis = 300;
		} else if (level.equals("4")) {
			dis = 300;
		} else if (level.equals("5")) {
			dis = 0;
		}

		return dis;

	}

}
